/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-03 14:14:47  
 * Copyright (c) 2015- joysuch-版权所有     
 */

package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.BpIndustrialPipelineBean;
import com.joysuch.wwyt.bp.bean.BpIndustrialPipelineListBean;
import com.joysuch.wwyt.bp.service.BpIndustrialPipelineService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@Api(tags = "设备设施管理-工业管道")
@RequestMapping("/bp/industrial")
public class BpIndustrialPipelineController {

    private static final Logger logger = LoggerFactory.getLogger(BpIndustrialPipelineController.class);

    @Autowired
    private BpIndustrialPipelineService bpIndustrialPipelineService;

    @RequestMapping("add")
    @ResponseBody
    public ResultBean add(@RequestBody @Valid BpIndustrialPipelineBean entity) {

            bpIndustrialPipelineService.add(entity);
            return ResultBean.success("");
    }

    @PostMapping("update")
    public ResultBean update(@RequestBody @Valid BpIndustrialPipelineBean entity) {

            if (entity.getId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            bpIndustrialPipelineService.update(entity);
            return ResultBean.success("");
    }

    @GetMapping("page")
    public ResultBean page(String name, Pageable page,Integer currentState) {
        try {

            page = PageDataUtils.addOrderByDesc(page, "id");
            Page<BpIndustrialPipelineListBean> data = bpIndustrialPipelineService.findByPage(page, name,currentState);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @PostMapping("delete")
    public ResultBean delete(Long id) {
        try {
            bpIndustrialPipelineService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    @GetMapping("getById")
    public ResultBean getById(Long id) {
        BpIndustrialPipelineBean byId = bpIndustrialPipelineService.getById(id);
        return ResultBean.wrapSuccessfulResult(byId);
    }

}
